package com.easyaop.core.interceptor;

import com.easyaop.api.Context;

import com.easyaop.api.around.*;
import com.easyaop.api.join.JoinPoint;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;

/**
 * @author lipan
 * @since 2025-02-22
 */
public class AroundDelegation {
    @RuntimeType
    public static Object intercept(@CustomOrigin("#t\\##m#d") String sig,
                                   @CustomOrigin Class<?> bean, @CustomTarget(optional = true) Object target,
                                   @AllArguments Object[] args,
                                   @CustomCall Call call) throws Exception {
        JoinPoint joinPoint = JoinPoint.of(bean, sig);
        for (Around around : Context.getArounds(sig)) {
            Call finalCall = call;
            call = () -> around.around(target, args, joinPoint, finalCall);
        }
        return call.call();
    }
}
